package com.bread.tliaswebmanagement.controller;

import com.bread.tliaswebmanagement.anno.Log;
import com.bread.tliaswebmanagement.pojo.Emp;
import com.bread.tliaswebmanagement.pojo.EmpQueryParam;
import com.bread.tliaswebmanagement.pojo.PageResult;
import com.bread.tliaswebmanagement.pojo.Result;
import com.bread.tliaswebmanagement.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        log.info("分页查询:{}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("新增员工:{}", emp);
        empService.add(emp);
        return Result.success();
    }

    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除员工:id{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询员工:{}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工:{}", emp);
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        log.info("查询所有员工");
        List<Emp> empList = empService.list();
        return Result.success(empList);
    }

}
